package com.ebeitech.library.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.ebeitech.library.ui.dialog.CommonAlertDialogFragment;
import com.ebeitech.library.ui.dialog.DialogActivity;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by alice on 2017/10/25.
 */
public class ViewUtil {

	/**
	 * Toast
	 *
	 * @param msg
	 */
	public static void toastMsg(Context context, String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * Toast
	 *
	 * @param res
	 */
	public static void toastMsg(Context context,int res) {
		Toast.makeText(context, res, Toast.LENGTH_SHORT).show();
	}

	public static void toastMsg(Context context,int res, long duration) {
		final Toast toast = Toast.makeText(context, res, Toast.LENGTH_SHORT);
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				toast.show();
			}
		}, 0, 2000);

		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				toast.cancel();
				timer.cancel();
			}
		}, duration);
	}

	public static CommonAlertDialogFragment showProgressDialog(Context c) {
		return showProgressDialog(c, "", "");
	}

	public static CommonAlertDialogFragment showProgressDialog(Context c, int titleResId, int messageResId) {
		return showProgressDialog(c, titleResId > 0 ? c.getString(titleResId) : "", messageResId > 0 ? c.getString(messageResId) : "");
	}

	public static CommonAlertDialogFragment showProgressDialog(Context c, String title, String message) {
		return showProgressDialog(c,title, message, false);
	}

	public static CommonAlertDialogFragment showProgressDialog(Context c, String title, String message, boolean cancelable) {
		final FragmentManager fm;
		if (c instanceof FragmentActivity) {
			fm = ((FragmentActivity) c).getSupportFragmentManager();
		} else {
			fm = null;
		}
		if (fm == null) {
			return null;
		}
		CommonAlertDialogFragment dialogFragment = new CommonAlertDialogFragment();
		dialogFragment.setCancelable(true);
		dialogFragment.setStyle(CommonAlertDialogFragment.DialogStyle.PROGRESS);
		dialogFragment.setTitle(title);
		if (!StringUtil.isStringNullOrEmpty(message)) {
			dialogFragment.setMessage(message);
		}
		dialogFragment.setCancelButtonVisible(false);
		dialogFragment.show(fm, "ProgressDialogFragment");

		return dialogFragment;
	}

	/**
	 *
	 * @param dialog
	 */
	public static void dismissDialog(CommonAlertDialogFragment dialog) {
		// TODO Auto-generated method stub
		if (dialog != null) {
			dialog.dismissAllowingStateLoss();
		}
	}

	/**
	 * 对话框永远显示的方法(不受activity影响,可以在receiver等里面开启)
	 * @param dialog
	 */
	public static void showDialog(Context context, CommonAlertDialogFragment dialog) {
		// TODO Auto-generated method stub
		DialogActivity.mDialogFragment = dialog;
		Intent intent = new Intent(context, DialogActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	/**
	 * AlertDialog只有一个按钮的情况
	 * @param c
	 * @param title
	 * @param message
	 * @param btnText
	 * @param onClickListener
	 * @return
	 */
	public static CommonAlertDialogFragment showAlertDialog(Context c, String title, String message, String btnText, CommonAlertDialogFragment.OnBtnClickListener onClickListener) {
		final FragmentManager fm;
		if (c instanceof FragmentActivity) {
			fm = ((FragmentActivity) c).getSupportFragmentManager();
		} else {
			fm = null;
		}
		if (fm == null) {
			return null;
		}
		CommonAlertDialogFragment dialogFragment = getAlertDialog(c, title, message, onClickListener, null, btnText, "");
		dialogFragment.setCancelButtonVisible(false);
		dialogFragment.show(fm, "CommonAlertDialogFragment");

		return dialogFragment;
	}

	public static CommonAlertDialogFragment showAlertDialog(Context c, String title, String message, CommonAlertDialogFragment.OnBtnClickListener okClickListener,
															CommonAlertDialogFragment.OnBtnClickListener cancelClickListener,
															String btnOkText, String btnCancelText) {
		final FragmentManager fm;
		if (c instanceof FragmentActivity) {
			fm = ((FragmentActivity) c).getSupportFragmentManager();
		} else {
			fm = null;
		}
		if (fm == null) {
			return null;
		}
		CommonAlertDialogFragment dialogFragment = getAlertDialog(c, title, message, okClickListener, cancelClickListener, btnOkText, btnCancelText);
		dialogFragment.show(fm, "CommonAlertDialogFragment");
		return dialogFragment;
	}

	public static CommonAlertDialogFragment getAlertDialog(Context c, String title, String message, CommonAlertDialogFragment.OnBtnClickListener okClickListener,
														   CommonAlertDialogFragment.OnBtnClickListener cancelClickListener,
														   String btnOkText, String btnCancelText) {
		CommonAlertDialogFragment dialogFragment = new CommonAlertDialogFragment();
		dialogFragment.setCancelable(false);
		dialogFragment.setTitle(title);
		if (!StringUtil.isStringNullOrEmpty(message)) {
			dialogFragment.setMessage(message);
		}
		if (!StringUtil.isStringNullOrEmpty(btnOkText) || !StringUtil.isStringNullOrEmpty(btnCancelText)) {
			dialogFragment.setButtonText(btnOkText, btnCancelText);
		}
		dialogFragment.setOkClickListener(okClickListener);
		dialogFragment.setCancelClickListener(cancelClickListener);

		return dialogFragment;
	}

	public static CommonAlertDialogFragment showAlertDialog(Context c, String title, String message, CommonAlertDialogFragment.OnBtnClickListener okClickListener, CommonAlertDialogFragment.OnBtnClickListener cancelClickListener) {
		return showAlertDialog(c, title, message, okClickListener, cancelClickListener, null, null);
	}

	public static CommonAlertDialogFragment showAlertDialog(Context c, int titleResId, int messageResId, CommonAlertDialogFragment.OnBtnClickListener okClickListener, CommonAlertDialogFragment.OnBtnClickListener cancelClickListener) {
		return showAlertDialog(c, c.getString(titleResId), c.getString(messageResId), okClickListener, cancelClickListener);
	}

	public static CommonAlertDialogFragment getSelectDialog(Context c, String title, CommonAlertDialogFragment.OnBtnClickListener selectClickListener, CommonAlertDialogFragment.OnBtnClickListener okClickListener, CommonAlertDialogFragment.OnBtnClickListener cancelClickListener) {
		CommonAlertDialogFragment dialogFragment = getAlertDialog(c, title, "", okClickListener, cancelClickListener, null, null);
		dialogFragment.setStyle(CommonAlertDialogFragment.DialogStyle.SELECT);
		dialogFragment.setSelectClickListener(selectClickListener);
		return dialogFragment;
	}

	public static CommonAlertDialogFragment showSelectDialog(Context c, String title, CommonAlertDialogFragment.OnBtnClickListener selectClickListener, CommonAlertDialogFragment.OnBtnClickListener okClickListener, CommonAlertDialogFragment.OnBtnClickListener cancelClickListener) {
		final FragmentManager fm;
		if (c instanceof FragmentActivity) {
			fm = ((FragmentActivity) c).getSupportFragmentManager();
		} else {
			fm = null;
		}
		if (fm == null) {
			return null;
		}
		CommonAlertDialogFragment dialogFragment = getSelectDialog(c, title, selectClickListener, okClickListener, cancelClickListener);
		dialogFragment.show(fm, "CommonSelectDialogFragment");
		return dialogFragment;
	}

	public static int dp2px(Context context, float dp) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dp * scale + 0.5f);
	}

	public static int px2dp(Context context, float px) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}

	public static DisplayMetrics getScreenSize(Activity activity){
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm;
	}

	/**
	 * 获取文字高度
	 *
	 * @param paint
	 * @return
	 */
	public static float getTextHeight(Paint paint) {
		return -paint.ascent() + paint.descent();
	}

	public static Point getCenterTextPonit(RectF rect, Paint paint, String text) {
		Point point = new Point();
		point.x = (int) rect.left + ((int) rect.width() >> 1)
				- ((int) paint.measureText(text) >> 1);
		point.y = (int) rect.bottom + (int) (-paint.ascent())
				- (int) getTextHeight(paint);
		point.y -= ((int) rect.height() >> 1)
				- ((int) getTextHeight(paint) >> 1);
		point.y += 1;
		return point;
	}

	public static DisplayMetrics getTextSize(int textSize, String text){
		DisplayMetrics dm = new DisplayMetrics();
		Paint paint = new Paint();
		paint.setTextSize(textSize);
		dm.widthPixels = (int) paint.measureText(text);
		Paint.FontMetrics fm = paint.getFontMetrics();
		dm.heightPixels = (int) (fm.descent - fm.ascent);
		return dm;
	}

	public static PopupWindow showPopupWindow(View contentView, View parent,
											  int width, int height, int gravity, int x, int y) {

		PopupWindow popupWindow = new PopupWindow(contentView, width, height);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setFocusable(true);
		popupWindow.showAtLocation(parent, gravity, x, y);
		return popupWindow;
	}

}
